<template>
	<view class="Recharge_content Transfer_content">
		<view class="kuang">
			<view class="title">
				<view class="inline-block left">
					<image src="../../static/image/left.png" mode=""  @tap="back"></image>
				</view>
			</view>
			<view class="vote_title">
				<text>{{i18n.Recharge_content}}</text>
			</view>
			<view class="choice" style="margin-top: 72upx;"  :style="i18n.register">
				<text >{{i18n.choice}}</text>
				<view class="picker" style="height: 100%;" :style="i18n.registerpieck">
					<view>{{ accountName }}</view>
					
					<image src="../../static/image/bottom.png" class="down" style="width: 24upx;height: 12upx;margin-left: 15upx;margin-top: 15upx;"></image>
				</view>
			</view>
		
			<view class="choice"  :style="i18n.register">
				<text >{{i18n.choice2}}</text>
				<view class="picker" style="height: 100%;" :style="i18n.registerpieck">
					<input type="text" class="nodeInfo" placeholder-class="candidatePlaceholder" maxlength="512" v-model="payloadInfo"/>
					<!-- <image src="../../static/image/bottom.png" class="down" style="width: 24upx;height: 12upx;margin-left: 15upx;"></image> -->
				</view>
			</view>
			
			<view class="choice" :style="i18n.register">
				<text >{{i18n.choice3}}</text>
				<view class="picker" style="height: 100%;" :style="i18n.registerpieck2">
					<input type="text"  :placeholder="i18n.dyps" disabled="disabled" maxlength="20" class="mortgageMount"  placeholder-class="candidatePlaceholder" placeholder-style="text-align:right;" style="width: 100%;margin-top:12upx"/>
					<!-- <image src="../../static/image/bottom.png" class="down" style="width: 24upx;height: 12upx;margin-left: 15upx;"></image> -->
				</view>
			</view>
			
			<password v-if="show" :gasPrice="100" :gasLimit="1000000" @close="close" @callback="callback" :yzpassword="i18n.yzpassword" :btnl="i18n.btn1" :btnr="i18n.btn2" :placeholder="i18n.placeholder" :passprice="i18n.passprice" :pwdtitle="i18n.pwdtitle"></password>
			
			
			<view class="bottombutton" @tap="registerHandler">
				{{i18n.zcbutton}}
			</view>
		</view>


	</view>
</template>

<script>
	import * as oexWeb3 from 'oex-web3';
	import BigNumber from 'bignumber.js';
	import password from '../../components/password/password.vue'
	import account from '../../common/account.js'
	export default {
		computed: {
			i18n() {
				return this.$t('user')
			}
		},
		data() {
			return {
				accountName:"",
				show: false,
				payloadInfo:"",
				oexBlance:""
			}
		},
		components:{ password },
		onLoad() {
			const accountInfo = uni.getStorageSync("account_info");
			this.accountName = accountInfo.accountName;
			this.$Web3Wrapper.getChainConfig().then(chainConfig => {
				oexWeb3.oex.setChainConfig(chainConfig);
			});
		},
		methods: {
			registerHandler(){
				// const _this=this
				const _this = this;
				if(this.payloadInfo == ""){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请填写节点信息':'Please fill in the node information');
					return;
				}
				//加载资产列表
				// const _this = this;
				account.getAccount(this.accountName).then(res =>{
					// const _this=this
					//console.log(res)
					const d = res.data.result.balances;
					if(d.length > 0){
						const oexAsset = d.filter(item =>{
							return item.assetID == 0
						})
						if(oexAsset.length < 1){
							_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '当前账户暂无OEX资产':'There is no oex asset in the current account')
						}else{
							const oexBlance = oexAsset[0].balance;
							const nodeInfo = uni.getStorageSync('nodeInfo');
							account.getCoin(nodeInfo,oexAsset[0].assetID).then(result =>{
								//console.log(result)
								const oexDecimals = result.data.result.decimals;
								_this.oexBlance = oexBlance/Math.pow(10,oexDecimals)
								//console.log(_this.oexBlance)
								if(Number(_this.oexBlance) < oexWeb3.oex.chainConfig.candidateMinQuantity){
									_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '当前账户OEX资产不足20万':'Oex assets in current account are less than 200000 yuan')
								}else{
									_this.show = true;
								}
							})
						}
						
					}else{
						_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '当前账户暂无资产':'There are no assets in the current account')
					}
					
					
				}).catch(error =>{
					 _this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '无法获取当前账户的资产信息,请稍后再试':'Unable to get the asset information of the current account. Please try again later');
				})
				
				
			},
			close(){
				//关闭支付框
				this.show = false;
			},
			callback(privateKey,gasPrice,gasLimit){
				/**
				* actionInfo = {accountName, remark, nonce}
				   > nonce: if not null, will be used in txInfo
				* gasInfo = {gasPrice, gasLimit}
				* payloadInfo = {url}
				* privateKey
				**/
				const _this = this;
				console.log(privateKey,gasPrice,gasLimit);
				this.show = false;
				
				const actionInfo = {
					accountName: this.accountName,
				};
				const	payloadInfo = { url:this.payloadInfo.trim() }
				const gasInfo = {gasPrice:"0x" + new BigNumber(gasPrice).toString(16), gasLimit: "0x" + new BigNumber(gasLimit).toString(16)};
				
				this.$Web3Wrapper.registerCandidate(actionInfo, gasInfo, payloadInfo, privateKey, () => { this.show = false; uni.showToast({
					// title:'注册成功',
					title:_this._i18n.locale == 'zh_CN' ? '注册成功':'login was successful',
					
				})}, (err) => {
					console.log(err)
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '发生错误:':'An error occurred:' + err);
				});			
				
			},
			back(){
				this.$CommonJS.navigateBack()
			}
		}
	}
</script>

<style lang="stylus">
	@import "../../static/stylus/nodeVote/node_vote.stylus"
	.nodeInfo,.mortgageMount,.candidatePlaceholder
		font-size 12px
		color #C2C9DF
</style>
